package com.bubbo.rpc.client;


import com.bubbo.discoverCenter.ServiceDiscover;
import com.bubbo.rpc.client.util.CollectionUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import java.util.Map;

/**
 * Created by waasdasd on 2017/6/15.
 */
public class ReferenceServer implements ApplicationContextAware, InitializingBean {

    private static final Logger logger = LoggerFactory.getLogger(ReferenceServer.class);


    private String serviceAddress;
    private ServiceDiscover serviceDiscover;
    public ReferenceServer(String serviceAddress, ServiceDiscover serviceDiscover) {
        this.serviceAddress = serviceAddress;
        this.serviceDiscover = serviceDiscover;
    }



    @Override
    public void afterPropertiesSet() throws Exception {


    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        //扫描所有消费服务标签
        Map<String, Object> serviceBeanMap = applicationContext.getBeansWithAnnotation(Reference.class);
        if (CollectionUtil.isNotEmpty(serviceBeanMap)) {
            for (Object object : serviceBeanMap.values()) {
                Reference reference = serviceBeanMap.getClass().getAnnotation(Reference.class);
                BubboProxy bubboProxy = applicationContext.getBean(BubboProxy.class);
                object=bubboProxy.create(reference.value().getClass());
                logger.info("发现服务:"+object);
            }
        }
    }
}
